<?php

namespace app\index\controller;

use app\admin\library\Auth;
use app\common\controller\Frontend;
use EasyWeChat\Factory;
use app\admin\model\Admin;
use app\common\controller\Api;
use app\admin\model\from\Teacher;
use app\admin\model\from\Family;
use app\admin\model\from\Promote;


class Sample extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
         $Api = new Api();
         //获取默认值
        $this->config = $Api->getConfig($Api->getDefault('Sample'));
          $this->auth = \app\admin\library\Auth::instance();
        $this->Teachermodel = new \app\admin\model\from\Teacher;
        $this->Familymodel = new \app\admin\model\from\Family;
        $this->Promotemodel = new \app\admin\model\from\Promote;
    }
 
    public function index($ids = null){
      
        if (!$this->auth->isLogin()){
            return redirect($this->request->domain().'/index/sample/login/ids/'.$ids);
        }
        if(!$ids) $this->error(__('链接有误'),"https://tmall.fc62.com/mobile/index.php",'','60');

        $id_ar = explode('___',$ids);
        if(!isset($id_ar[1]) || !isset($id_ar[0])) $this->error(__('链接有误'),'','60');
        $id_ar[0] = passport_decrypt($id_ar[0]);
        $id_ar[1] = passport_decrypt($id_ar[1]);
        $info = model(GetTableModel(Sample_arr()[$id_ar[1]]['model']))->field('id,tel,receive')->where(['id'=>$id_ar[0]])->find();
        if($info['receive'] == 1){
          $arr['receive'] = 2;
          $arr['receive_time'] = time();
          $arr['sweep_publish'] = \think\Session::get('admin')['userid'];
          $result = model(GetTableModel(Sample_arr()[$id_ar[1]]['model']))->save($arr,['id'=>$id_ar[0]]);
          if($result){
            $this->success("领取成功","https://tmall.fc62.com/mobile/index.php",'','60');
          }
        }else{
          $this->error("二维码已被领取","https://tmall.fc62.com/mobile/index.php",'','60');
        }
        $this->view->assign("receive", $info['receive']);
    }  
    public function login($ids = null){
        $app = Factory::work($this->config);
        $token = $app->access_token->getToken();//获取企业微信token
        $code = $this->request->request('code');
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$token['access_token']}&code={$code}";
        $user = \fast\Http::get($url);
        if(!empty($user)) $user = json_decode($user,true);
        
        if($user['errcode'] == 0){
            if(isset($user['UserId'])){
                if(!$this->auth->IndexLogin($user['UserId'])){
                    $this->error(__('您的账户不存在，请联系管理员'));
                }else{
                    //print_r(\think\Session::get('admin')['userid']);die;
                    return redirect($this->request->domain().'/A/'.$ids);
                }
            }
        }else{
            return redirect($this->request->domain().'/index/sample/oauth/ids/'.$ids);
        }
    }
    public function oauth($ids = null){
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->config['corp_id']."&redirect_uri=".$this->config['oauth']['callback'].'/ids/'.$ids."&response_type=code&scope=".$this->config['oauth']['scopes']."&agentid=".$this->config['agent_id']."&state=STATE#wechat_redirect";
        return redirect($url);
    }
   public function teacher_mobile(){
       $tel = $this->request->post("tel");
       $teacher = Teacher::get(['tel' => $tel]);
       $family = Family::get(['tel' => $tel]);
       $promote = Promote::get(['tel' => $tel]);
       if($teacher||$family||$promote){
        return 1;
       }else{
        return 0;
       }
        
    }  
    public function teacher_add(){
       $row = $this->request->post();
       if ($this->request->isPost()) {
                $params = $this->request->post();
                if ($params) {
                   $result =  $this->Teachermodel->save($params);
                   if ($result !== false) {
                        return 1;
                    } else {
                        return 0;
                    }
                  
                }
                return 0;
            }
    }   
    public function family_mobile(){
       $tel = $this->request->post("tel");
       $teacher = Teacher::get(['tel' => $tel]);
       $family = Family::get(['tel' => $tel]);
       $promote = Promote::get(['tel' => $tel]);
       if($teacher||$family||$promote){
        return 1;
       }else{
        return 0;
       }
        
    }  
    public function family_add(){
       $row = $this->request->post();
       if ($this->request->isPost()) {
                $params = $this->request->post();
                if ($params) {
                   $result =  $this->Familymodel->save($params);
                   if ($result !== false) {
                        return 1;
                    } else {
                        return 0;
                    }
                  
                }
                return 0;
            }
    }
    public function promote_mobile(){
       $tel = $this->request->post("tel");
       $teacher = Teacher::get(['tel' => $tel]);
       $family = Family::get(['tel' => $tel]);
       $promote = Promote::get(['tel' => $tel]);
       if($teacher||$family||$promote){
        return 1;
       }else{
        return 0;
       }
        
    }  
    public function promote_add(){
       $row = $this->request->post();
       if ($this->request->isPost()) {
                $params = $this->request->post();
                if ($params) {
                   $result =  $this->Promotemodel->save($params);
                   if ($result !== false) {
                        return 1;
                    } else {
                        return 0;
                    }
                  
                }
                return 0;
            }
    }
    public function family_code($ids = null){
      //print_r(passport_encrypt('17757').'__'.passport_encrypt('2'));die;
        if(!$ids) $this->error(__('链接有误'));
        $id_ar = explode('___',$ids);

        if(!isset($id_ar[1]) || !isset($id_ar[0])) $this->error(__('链接有误'),'','60');
        $id_ar[0] = passport_decrypt($id_ar[0]);
        $id_ar[1] = passport_decrypt($id_ar[1]);

        $info = model(GetTableModel(Sample_arr()[$id_ar[1]]['model']))->field('id,tel,receive')->where(['id'=>$id_ar[0]])->find();
        if(!$info) $this->error(__('信息不存在'));
        $this->view->assign("receive", $info['receive']);
        $this->view->assign("erweima", 'http://qr.liantu.com/api.php?bg=ffffff&fg=0000&text=http://oa.fc62.com/A/'.urlencode($ids));
        
        return $this->view->fetch();
    }

    

}
